#! /bin/sh

# Get proofd config
[ -f /etc/sysconfig/xproofd ] && . /etc/sysconfig/xproofd

PROOFD=$ROOTSYS/bin/xproofd

# Source function library.
. /etc/init.d/functions

# Get config.
. /etc/sysconfig/network

MYLOGDIR=`dirname $XPRFLOG`
if [ ! -d $MYLOGDIR ];then
  mkdir -p $MYLOGDIR
  chown $ALICE_PROOF_AAF_USER:$ALICE_PROOF_AAF_GROUP $MYLOGDIR
fi

# Check that networking is up.
if [ ${NETWORKING} = "no" ]
then
	exit 0
fi

[ -x $PROOFD ] || exit 0

RETVAL=0
prog="xproofd"

start() {
	echo -n $"Starting $prog: "
	#killall -9 proofserv.exe > /dev/null 2>&1
	if [ "x$ALICE_PROOF_AAF_DIR" == "x" ];then
	  echo "\$ALICE_PROOF_AAF_DIR is not defined !!! Make sure that you sourced aaf.cf file !!!"
	  return 1
	fi

  daemon $PROOFD -c $XPRFCONF -b -l $XPRFLOG $XPRFDEBUG
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/xproofd
	return $RETVAL
}

stop() {
  test ! -f /var/lock/subsys/xproofd && return 0 || true
  echo -n $"Stopping $prog: "
  killproc xproofd
  RETVAL=$?
	#killall -9 proofserv.exe > /dev/null 2>&1
	find $ALICE_PROOF_AAF_PROOF_DIR/proofbox -type f -atime +7 -exec rm -f {} \;
	find $ALICE_PROOF_AAF_PROOF_DIR/logs -type f -atime +7 -exec rm -f {} \;
	find $ALICE_PROOF_AAF_PROOF_DIR/proofbox -depth -type d -empty -exec rmdir {} \;
  echo
  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/xproofd
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status xproofd
	;;
  restart|reload)
	stop
	start
	;;
  restart_if_down)
  MYRETVAL=`ps aux | grep bin/xproofd | grep -v grep`
  MYRETVAL=$?
  if [ "$MYRETVAL" != 0 ]; then
    stop
    start
  fi
  ;;

  condrestart)
	if [ -f /var/lock/subsys/proofd ]; then
            stop
            start
        fi
	;;
  *)
	echo  $"Usage: $0 {start|stop|status|restart|reload|condrestart}"
	exit 1
esac

exit $RETVAL
